//
//  UserAccount.swift
//  swift_learning
//
//  Created by aron on 2017/2/1.
//  Copyright © 2017年 arons. All rights reserved.
//

import Foundation

class UserAccount : NSObject , NSCoding{
    
    /// 用户ID
    var uid : String?
    
    /// 过期时间
    var expires_in : Int = 0 {
        didSet {
            expiresDate =  NSDate(timeIntervalSinceNow: TimeInterval(expires_in))
        }
    }
    
    /// AccessToken
    var access_token : String?
    
    /// 过期日期
    var expiresDate : NSDate?
    
    /// 昵称
    var screen_name : String?

    /// 头像
    var avatar_large : String?
    
    init(dict: [String : Any]) {
        super.init()
        
        // 使用KVC设置属性值
        setValuesForKeys(dict)
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
    
    // MARK: - ......::::::: public :::::::......
    
    // MARK: - ......::::::: NSCoding :::::::......
    required init?(coder aDecoder: NSCoder) {
        uid = aDecoder.decodeObject(forKey: "uid") as! String?
        access_token = aDecoder.decodeObject(forKey: "access_token") as! String?
        expiresDate = aDecoder.decodeObject(forKey: "expiresDate") as! NSDate?
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as! String?
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as! String?
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expiresDate, forKey: "expiresDate")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
}
